package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.LoginLog;
import cn.edu.ujn.shi.service.ILoginLogService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: YSJ
 * @date 2024-05-31 08:42
 * @ClassName: LoginLogController
 */
@Controller
public class LoginLogController {

    @Autowired
    private ILoginLogService loginLogService;

    @RequestMapping("/loginLog")
    public String doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");//获取username框中的用户名
        List<LoginLog> logs = loginLogService.getLoginLog(username);
        //通过loginLogService获取指定用户名的登录日志，并将其存储在一个列表（List）中
        request.setAttribute("logs", logs);
        return "loginLog";
    }

    @RequestMapping("deleteLoginLogServlet")
    public void deleteLoginLogServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");//获取id框中的用户名
        loginLogService.delete(Integer.parseInt(id));//是一个静态方法，用于将字符串转换为整数
        response.sendRedirect("loginLog");//重定向
    }


}
